package wxshop.web.admin.tools;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import wxshop.api.admin.model.wx.Access_token;

/**
 * Created by 121874694@qq.com on 2018/3/4.
 */

public class GetToken {
	public static void getToken() throws Exception {
		ObjectMapper objectMapper = new ObjectMapper();
		long time = System.currentTimeMillis();
		if(ConstValues.TOKEN==null||ConstValues.TIME == 0||(ConstValues.TIME-time)>7200*1000){
			String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ConstValues.APP_ID+"&secret="+ConstValues.APP_SECRET;
			String gt = HttpUtil.doGet(url,ConstValues.CHARSET);
			Access_token at = objectMapper.readValue(gt, Access_token.class);
			ConstValues.TOKEN =at.getAccess_token();
			ConstValues.TIME = System.currentTimeMillis();
			System.out.println(at.getAccess_token());
		}
	}
}
